﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Data;
using UCL.Forms;

namespace DBKit5.UControls
{
    /// <summary>
    /// 数据表对象集
    /// </summary>
    /// <remarks>2014-1-21 by sutroon</remarks>
    public partial class ucTableObjects : UserControl
    {
        /// <summary>
        /// 设置或获取接收Sql命令语句的委托方法
        /// <para>如创建表结构语句等</para>
        /// <para>ShowSqlHandler(string sql,bool isExec)</para>
        /// </summary>
        public Action<string,bool> ShowSqlHandler { get; set; }

        /// <summary>
        /// 设置或获取树形列表选择后的委托方法
        /// <para>用于更换对象名称等,参数1是对象名称(objName)，参数2是对象类型(objType,包含TABLE|PROCEDURE|VIEW)</para>
        /// </summary>
        public Action<string, string> AfterSelectHandler { get; set; }

        /// <summary>
        /// 获取或设置数据库帮助类的实例对象
        /// </summary>
        public IDatabaseHelper DBH { get; set; }
        public ucTableObjects()
        {
            InitializeComponent();
            cmsiClickConfig();
        }

        private void tvTables_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvTables.SelectedNode.Level == 0)
            {
                foreach (ToolStripMenuItem c in cmsMain.Items)
                {
                    c.Visible = false;
                }
                cmi新建ToolStripMenuItem.Visible = true;
                cmi刷新ToolStripMenuItem.Visible = true;
                return;
            }
            foreach (ToolStripMenuItem c in cmsMain.Items)
            {
                c.Visible = true;
            }
            object tag = tvTables.SelectedNode.Tag;
            if (tag == null) return;
            switch (tag.ToString())
            {
                case "TABLE":
                    cmi执行ToolStripMenuItem.Visible = false;
                    break;
                case "PROCEDURE":
                    cmiCURDToolStripMenuItem.Visible = false;
                    break;
                case "VIEW":
                    cmiCURDToolStripMenuItem.Visible = false;
                    break;
            }
            if (AfterSelectHandler != null) AfterSelectHandler(tvTables.SelectedNode.Text, tag.ToString());
        }

        private void cmsiClickConfig()
        {
            cmiCURDToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi编辑ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi复制名称ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi清空ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi删除ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi生成代码ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi刷新ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi新建ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi执行ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi重命名ToolStripMenuItem.Click += new EventHandler(cmi_Click);
        }

        // cmsViewsList
        private void cmi_Click(object s, EventArgs e)
        {
            ToolStripItem mi = (ToolStripItem)s;
            TreeView tv = mi.GetSourceControl<TreeView>();
            if (tv == null || tv.SelectedNode == null) return;
            string nodeType =tv.SelectedNode.Level == 0?tv.SelectedNode.Text: tv.SelectedNode.Parent.Text; // 表 | 视图 | 存储过程            
            string itemName = tv.SelectedNode.Text;
            // 通用菜单
            switch (mi.Name)
            {
                case "cmi复制名称ToolStripMenuItem":
                    Clipboard.SetText(itemName);
                    break;
                case "cmi刷新ToolStripMenuItem":
                    this.LoadTables();
                    break;
            }
            // 视图专用菜单
            if (nodeType == "视图")
            {
                switch (mi.Name)
                {
                    case "cmi新建ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetCreateViewSql(),false);
                        break;
                    case "cmi编辑ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SO_GetCreateViewStructures(itemName)[itemName], false);
                        break;
                    case "cmi删除ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetDropViewSql(), false);
                        break;
                    case "cmi执行ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetExecViewSql(itemName), true);
                        break;
                    case "cmi重命名ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetRenameViewSql(), false);
                        break;
                }
                return;
            }

            // 存储过程专用菜单
            if (nodeType == "存储过程")
            {
                switch (mi.Name)
                {
                    case "cmi新建ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetCreateProcedureSql(), false);
                        break;
                    case "cmi编辑ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SO_GetCreateProcedureStructures(itemName)[itemName], false);
                        break;
                    case "cmi删除ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetDropProcedureSql(), false);
                        break;
                    case "cmi执行ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetExecProcedureSql(itemName),true);
                        break;
                    case "cmi重命名ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetRenameProcedureStructureSql(), false);
                        break;
                }
                return;
            }

            // 数据表专用菜单
            if (nodeType == "表")
            {
                switch (mi.Name)
                {
                    case "cmi新建ToolStripMenuItem":
                        if (ShowSqlHandler != null) ShowSqlHandler(DBH.SQL_GetCreateTableSql(), false);
                        break;
                    case "cmi编辑ToolStripMenuItem":
                        if (DBH != null && ShowSqlHandler != null) ShowSqlHandler(DBH.Sql_GetShowStructureSql(itemName), false);
                        break;
                    case "cmiCURDToolStripMenuItem":
                        new frmCURDForm() { TableName = itemName, DBH = DBH, ExecSqlHandler = ShowSqlHandler }.Show();
                        break;
                    case "cmi复制名称ToolStripMenuItem":
                        Clipboard.SetText(itemName);
                        break;
                    case "cmi清空ToolStripMenuItem":
                        if (DBH != null && ShowSqlHandler != null) ShowSqlHandler(DBH.sql_GetTruncateTableSql(itemName), false);
                        break;
                    case "cmi删除ToolStripMenuItem":
                        if (DBH != null && ShowSqlHandler != null) ShowSqlHandler(DBH.Sql_GetDropTableSql(itemName), false);
                        break;
                    case "cmi重命名ToolStripMenuItem":
                        if (DBH != null && ShowSqlHandler != null) ShowSqlHandler(DBH.Sql_GetRenameTableSql(itemName, "{new_table_name}"), false);
                        break;
                    case "cmitl刷新ToolStripMenuItem":
                        if (DBH != null) this.LoadTables();
                        break;
                    case "cmi生成代码ToolStripMenuItem":
                        this.OpenDialog<frmGenerateSqlData>(() => new frmGenerateSqlData() { TableName = itemName, Dbh = DBH });
                        break;
                }
                return;
            }
        }

        /// <summary>
        /// 加载所有数据表
        /// </summary>
        /// <param name="idh"></param>
        public void LoadTables()
        {
            if (this.DBH == null) return;
            tvTables.Nodes.Clear();
            // 表
            TreeNode tnTables = new TreeNode("表") { Tag = "TABLE" };
            foreach (string s in DBH.SO_GetTables())
            {
                tnTables.Nodes.Add(new TreeNode(s) { Tag = "TABLE" });
            }
            tvTables.Nodes.Add(tnTables);
            tvTables.Nodes[0].Expand();
            if (DBH.MessageSet.Count > 0) MessageBox.Show(DBH.MessageSet.ToString(Environment.NewLine));
            // 存储过程
            TreeNode tnProcedures = new TreeNode("存储过程") { Tag = "PROCEDURE" };
            foreach (string s in DBH.SO_GetProcedures())
            {
                tnProcedures.Nodes.Add(new TreeNode(s) { Tag = "PROCEDURE" });
            }
            tvTables.Nodes.Add(tnProcedures);
            // 视图
            TreeNode tnViews = new TreeNode("视图") { Tag = "VIEW" };
            foreach (string s in DBH.SO_GetViews())
            {
                tnViews.Nodes.Add(new TreeNode(s) { Tag = "VIEW" });
            }
            tvTables.Nodes.Add(tnViews);
        }
    }
}
